﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Yule8xCommon.Applib;
using Yule8xCommon.Db;
using Yule8xCommon.Typelib;

namespace Yule8xModel
{
    public class BozhongGameUtil
    {

        /// <summary>
        /// 写入MoneyValue表
        /// <para>返回记录ID</para>
        /// </summary>
        /// <param name="number"></param>
        /// <param name="amount"></param>
        /// <returns></returns>
        public static int CheckMoneyValue(int number, decimal amount)
        {
            // 如果moneyvalue数量不存在，则先往库里面写入一条记录
            string sqlstmt = "select ID from Bzw_WMoneyValue where Wallet=" + number;
            object oid = DbFactory.Init(AppInfo.con_bozhong).GetValue(sqlstmt);
            if (null == oid)
            {
                sqlstmt = string.Format("INSERT INTO [Bzw_WMoneyValue] ([Value],[Wallet],[InputDate],[IsEnable],[PayType],[RoomCard],[Description]) VALUES ({0},{1},'{2}',1,0,{1},'用户自填数量')", amount, number, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                DbFactory.Init(AppInfo.con_bozhong).ExecuteNonQuery(sqlstmt);
                //oid = DbFactory.Init(AppInfo.con_bozhong).GetValue("SELECT @@IDENTITY");
            }
            return null == oid ? 0 : int.Parse(oid.ToString());
        }
        public static DataTable GetMoneyValueTable(string idField = "ID", string valueField = "Value", string textField = "Wallet", int limit = 6)
        {
            string sqlstmt = string.Format("select top {3} [ID] as [{0}], [Value] as [{1}], [Wallet] as [{2}] FROM [Bzw_WMoneyValue] where PayType > 0 order by [Wallet]", idField, valueField, textField, limit);
            return DbFactory.Init(AppInfo.con_bozhong).ExecuteDataTable(sqlstmt);
        }
        /// <summary>
        /// 失败返回-1
        /// </summary>
        /// <param name="game_id"></param>
        /// <returns></returns>
        public static int GetRoomCard(int game_id)
        {
            string sqlstmt = string.Format("select RoomCard from TUserProperty where UserID={0}", game_id);
            return int.Parse(DbFactory.Init(AppInfo.con_bozhong).GetValue(sqlstmt, "-1").ToString());
        }
        public static bool UpdateRoomCard(int game_id, int number)
        {
            // 更新数据库
            string sqlstmt = string.Format("update TUserProperty set RoomCard=RoomCard+{0} where UserID={1}", number, game_id);
            int n = DbFactory.Init(AppInfo.con_bozhong).ExecuteNonQuery(sqlstmt);
            LogUtil.Instance.WriteInfo(string.Format("[更新泊众房卡数量] {0}:{1}", n, sqlstmt));
            bool result = n > 0;
            return result;
        }
        // 失败返回空字符串
        public static string GetNickName(int game_id)
        {
            string sqlstmt = string.Format("select NickName from TUserInfo where UserID='{0}'", game_id);
            object value = DbFactory.Init(AppInfo.con_bozhong).GetValue(sqlstmt);
            return null == value ? string.Empty : value.ToString();
        }
        /// <summary>
        /// 获取游戏ID
        /// </summary>
        /// <param name="unionid"></param>
        /// <returns></returns>
        public static int GetGameID(string unionid)
        {
            if (string.IsNullOrEmpty(unionid))
            {
                return 0;
            }
            string sqlstmt = string.Format("select UserID from TUsers where unionid='{0}'", unionid);
            string value = DbFactory.Init(AppInfo.con_bozhong).GetValue(sqlstmt, "0").ToString();
            return StringUtil.ToInteger(value);
        }

        public static bool CheckGameId(int game_id)
        {
            string sqlstmt = string.Format("select count(0) from TUsers where UserID ={0}", game_id);
            int count = DbFactory.Init(AppInfo.con_bozhong).ExecuteCount(sqlstmt);
            return count > 0;
        }
        /*
/// <summary>
/// 写入MoneyValue表
/// <para>返回记录ID</para>
/// </summary>
/// <param name="number"></param>
/// <param name="amount"></param>
/// <returns></returns>
public static bool CheckMoneyValue(int number, int amount, out string return_msg)
{
   string url = AppInfo.GameHostApiUri_Bozhong;
   // 请求字符串
   string paramstr = string.Format("action=addmoneyvalue&amount={0}&number={1}", amount, number);
   // 数据加密签名
   string signstr = MD5Util.GetShortMD5(string.Format("{0}&key={1}", paramstr, AppInfo.EncryptKey));
   // 返回信息
   return_msg = HttpUtil.HttpGet(url, string.Format("{0}&sign={1}", paramstr, signstr)); // {"status":True,"text":""}
   //msg = "{\"code\":null,\"name\":null,\"status\":false,\"text\":\"提交的参数不完整\"}";
   return return_msg.ToLower() == "success";
}
/// <summary>
/// 更新游戏房卡
/// </summary>
/// <param name="game_id"></param>
/// <param name="number"></param>
/// <param name="return_msg"></param>
/// <returns></returns>
public static bool UpdateRoomCard(int game_id, int number, out string return_msg)
{
   string url = AppInfo.GameHostApiUri_Bozhong;
   // 请求字符串
   string paramstr = string.Format("action=addroomcard&uid={0}&number={1}", game_id, number);
   // 数据加密签名
   string signstr = MD5Util.GetShortMD5(string.Format("{0}&key={1}", paramstr, AppInfo.EncryptKey));
   // 返回信息
   return_msg = HttpUtil.HttpGet(url, string.Format("{0}&sign={1}", paramstr, signstr)); // {"status":True,"text":""}
   //msg = "{\"code\":null,\"name\":null,\"status\":false,\"text\":\"提交的参数不完整\"}";
   return return_msg.ToLower() == "success";
}

public static DataTable GetMoneyValueTable()
{
   string url = AppInfo.GameHostApiUri_Bozhong;
   // 请求字符串
   string paramstr = "action=getmoneyvaluetable";
   // 数据加密签名
   string signstr = MD5Util.GetShortMD5(string.Format("{0}&key={1}", paramstr, AppInfo.EncryptKey));
   // 返回信息
   string msg = HttpUtil.HttpGet(url, string.Format("{0}&sign={1}", paramstr, signstr)); // {"status":True,"text":""}
   //msg = "{\"code\":null,\"name\":null,\"status\":false,\"text\":\"提交的参数不完整\"}";
   try
   {
       string error;
       DataSet ds = XMLUtil.ConvertXMLToDataSet(msg, out error);
       return (null == ds) ? null : ds.Tables[0];
   }
   catch (Exception ex)
   {
       LogUtil.Instance.WriteError("GetMoneyValue出错:" + ex.Message, ex);
   }
   return null;
}

/// <summary>
/// 获取游戏昵称
/// <para>失败返回空</para>
/// </summary>
/// <param name="game_id"></param>
/// <returns></returns>
public static string GetNickname(int game_id)
{
   string url = AppInfo.GameHostApiUri_Bozhong;
   // 请求字符串
   string paramstr = string.Format("action=getnickname&uid={0}", game_id);
   // 数据加密签名
   string signstr = MD5Util.GetShortMD5(string.Format("{0}&key={1}", paramstr, AppInfo.EncryptKey));
   // 返回信息
   string return_msg = HttpUtil.HttpGet(url, string.Format("{0}&sign={1}", paramstr, signstr));
   return return_msg;
}

/// <summary>
/// 获取游戏库GameID
/// </summary>
/// <param name="unionid"></param>
/// <returns></returns>
internal static int GetGameID(string unionid,out string error)
{

   string url = AppInfo.GameHostApiUri_Bozhong;
   // 请求字符串
   string paramstr = string.Format("action=getgameid&unionid={0}", unionid);
   // 数据加密签名
   string signstr = MD5Util.GetShortMD5(string.Format("{0}&key={1}", paramstr, AppInfo.EncryptKey));
   // 返回信息
   string return_msg = HttpUtil.HttpGet(url, string.Format("{0}&sign={1}", paramstr, signstr));
   int iout;
   if(int.TryParse(return_msg, out iout))
   {
       error = string.Empty;
       return iout;
   }else
   {
       error = return_msg;
       return 0;
   }
}

/// <summary>
/// 获取游戏房卡数量
/// <para>-1表示失败</para>
/// </summary>
/// <param name="game_id"></param>
/// <returns></returns>
public static int GetRoomCard(int game_id)
{
   string url = AppInfo.GameHostApiUri_Bozhong;
   // 请求字符串
   string paramstr = string.Format("action=getroomcard&uid={0}", game_id);
   // 数据加密签名
   string signstr = MD5Util.GetShortMD5(string.Format("{0}&key={1}", paramstr, AppInfo.EncryptKey));
   // 返回信息
   string return_msg = HttpUtil.HttpGet(url, string.Format("{0}&sign={1}", paramstr, signstr));
   int iout;
   return int.TryParse(return_msg, out iout) ? iout : 0;
}
*/

    }
}
